<?php 

include './Http.class.php';
include './token.class.php';
include './pdo.php';


if(!$access_token=Token::gettoken('./token.txt'))
{
$access_url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx397175932d2db7f2&secret=e1464f10daa4456a12161fe3a7d3add1';
$access_data=Http::get($access_url);
$accessArr=json_decode($access_data,true);
$access_token=$accessArr['access_token'];
//写入文件
// Token::settoken('./token.txt',$access_token);
}



echo $access_token;
$url="https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$access_token}&type=image";
echo $url;
echo '<br>';

$filepath=dirname(__FILE__).'/image/5.jpg';
echo $filepath;
echo '<hr>';
// $filedata= realpath($filepath) ;
if(class_exists('\CURLFile'))
{
	$filedata=array('filedname'=> new \CURLFile(realpath($filepath)));
}else
{
	$filedata=array( 'filedname' => '@'.realpath($filepath));
};
var_dump($filedata);
echo '<hr>';
$res=Http::post($url,$filedata);
$resArr = json_decode($res, true);
var_dump($res);
$sql="insert into material(type,media_id,created_at) values(:type,:media_id,:created_at)";
echo '<hr>';
echo $sql;
$stmt = $pdo -> prepare($sql);
$res = $stmt -> execute($resArr);
if($res)
{
    echo '上传成功';
}else
{
    echo '上传失败';
}

 ?>